package com.ljd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljd.entity.EduTeacher;
import com.ljd.entity.page.PageResponse;
import com.ljd.entity.vo.TeacherQuery;
import com.ljd.mapper.EduTeacherMapper;
import com.ljd.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.codec.language.bm.Lang;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author ljd
 * @since 2021-10-20
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public void pageQuery(Page<EduTeacher> page, TeacherQuery teacherQuery) {
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        if (teacherQuery == null) {
            baseMapper.selectPage(page, null);
            return;
        }
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }


        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }

        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_modified", end);
        }
        queryWrapper.orderByDesc("gmt_create");

        baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public PageResponse<EduTeacher> getTeachers(Long currentPage, Long pageSize) {
        Page<EduTeacher> page = new Page<>(currentPage, pageSize);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_modified");
        baseMapper.selectPage(page, wrapper);
        PageResponse<EduTeacher> response = new PageResponse<>();
        response.setItems(page.getRecords());
        response.setCurrent((int) page.getCurrent());
        response.setPageSize((int) page.getSize());
        response.setTotal((int) page.getTotal());
        response.setHasNext(page.hasNext());
        response.setHasPrevious(page.hasPrevious());
        response.setPages((int) page.getPages());
        return response;
    }

}
